package main

import (
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/go-sql-driver/mysql"
)


type Count struct {
	ID int
	Count int
}

func inc(db *gorm.DB, t string, msg chan string) {
	fmt.Println(t)
	for i := 0; i < 10000; i++ {
		if err := db.Exec("update counts set count = count + 1 where id = 1").Error; err != nil {
			fmt.Println(err)
		}
	}
	msg <- "ok"
}

func main() {
	// 注意这个 dsn 的格式
	db, err := gorm.Open("mysql", "vagrant:vagrant@tcp(vagrant:3306)/test?charset=utf8&parseTime=True&loc=Local")
	if err != nil {
		fmt.Printf("连接数据库失败: %s\n", err)
	}
	db.LogMode(true)

	count := 0
	if err := db.Model(&Count{}).Where("count = 1").Count(&count); err != nil {

	}

	//db.Exec("update counts set count = 0 where id = 1")
	//messages1 := make(chan string)
	//messages2 := make(chan string)
	//messages3 := make(chan string)
	//messages4 := make(chan string)
	//messages5 := make(chan string)
	//
	//go inc(db, "1", messages1)
	//go inc(db, "2", messages2)
	//go inc(db, "3", messages3)
	//go inc(db, "4", messages4)
	//go inc(db, "5", messages5)
	//
	//<-messages1
	//<-messages2
	//<-messages3
	//<-messages4
	//<-messages5

	db.Close()
}
